// InputNumDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SignalAnalyzer.h"
#include "InputNumDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInputNumDlg dialog


CInputNumDlg::CInputNumDlg(const UINT uId, const CString strCaption, CWnd* pParent /*=NULL*/)
	: CDialog(CInputNumDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInputNumDlg)
	m_strValue = _T("");
	//}}AFX_DATA_INIT
	m_uId = uId;
	m_strCaption = strCaption;
}


void CInputNumDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInputNumDlg)
	DDX_Control(pDX, IDC_COMBO_UNIT, m_comboUnit);
	DDX_Text(pDX, IDC_EDIT_VALUE, m_strValue);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CInputNumDlg, CDialog)
	//{{AFX_MSG_MAP(CInputNumDlg)
	//}}AFX_MSG_MAP
	ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON_ENTER, OnButtonClicked)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInputNumDlg message handlers

void CInputNumDlg::OnButtonClicked(UINT uId){
	/*
	UINT index = uId - IDC_BUTTON1;
	CString num;
	num.Format(_T("%d"),index);
	AfxMessageBox(num);
	*/

	CString entryStr = GetEntryStr(m_uId);
	CString unitSelectName;
	m_comboUnit.GetWindowText(unitSelectName);

	switch(uId){
	case IDC_BUTTON_BACKSPACE:
		if (m_strValue.GetLength()>0){
			m_strValue = m_strValue.Left(m_strValue.GetLength() - 1);
		}
		break;
	case IDC_BUTTON_CLEAR:
		m_strValue = _T("");
		break;
	case IDC_BUTTON_ENTER:
		AfxGetApp()->WriteProfileString(PROFILE_SECTION, entryStr, m_strValue);
		AfxGetApp()->WriteProfileInt(PROFILE_SECTION, entryStr + PROFILE_ENTRY_INDEX_SUFFIX, m_comboUnit.GetCurSel());
		AfxGetApp()->WriteProfileString(PROFILE_SECTION, entryStr + PROFILE_ENTRY_NAME_SUFFIX, unitSelectName);
		EndDialog(IDOK);
		break;
	default:
		CButton* btn = (CButton*)GetDlgItem(uId);
		CString btnCaption;
		btn->GetWindowText(btnCaption);
		m_strValue += btnCaption;
	}
	UpdateData(false);

}

BOOL CInputNumDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	SetWindowText(m_strCaption);

	CString entryStr = GetEntryStr(m_uId);
	int defaultIndex;
	CString defaultValue;
	switch (m_uId){
	case IDC_BUTTON_START_FREQUENCY:
		defaultValue = DEFAULT_START_FREQUENCY;
		defaultIndex = DEFAULT_FREQUENCY_INDEX;
		break;
	case IDC_BUTTON_STOP_FREQUENCY:
		defaultValue = DEFAULT_STOP_FREQUENCY;
		defaultIndex = DEFAULT_FREQUENCY_INDEX;
		break;
	case IDC_BUTTON_AMPLITUDE:
		defaultValue = DEFAULT_AMPLITUDE;
		defaultIndex = DEFAULT_AMPLITUDE_INDEX;
		break;
	}
	SetComboUnitDropDownValues(m_uId, m_comboUnit);
	m_comboUnit.SetCurSel(
		AfxGetApp()->GetProfileInt(PROFILE_SECTION, entryStr + PROFILE_ENTRY_INDEX_SUFFIX, defaultIndex)
	);
	m_strValue = AfxGetApp()->GetProfileString(PROFILE_SECTION, entryStr, defaultValue);
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

extern CString GetEntryStr(UINT uId)
{
	CString strId;
	strId.Format(_T("%d"),uId);
	return strId;
}

static void SetComboUnitDropDownValues(UINT uId, CComboBox& comboUnit){
	switch (uId){
	case IDC_BUTTON_START_FREQUENCY:
	case IDC_BUTTON_STOP_FREQUENCY:
		comboUnit.AddString("Hz");
		comboUnit.AddString("KHz");
		comboUnit.AddString("MHz");
		comboUnit.AddString("GHz");
		break;
	case IDC_BUTTON_AMPLITUDE:
		comboUnit.AddString("dBm");
		comboUnit.AddString("dBV");
		comboUnit.AddString("mW");
		comboUnit.AddString("mV");
		comboUnit.AddString("dB");
		break;
	}
}
